import http from '@/api/http'
import type {
    APIResult,
    APIPaging,
    PaginationParams,
    SupplierDTO,
    SupplierQueryParams,
    SupplierCreateParams
} from '@/api/types'

export const supplierAPI = {
    // 获取供应商列表
    getList: (
        params: PaginationParams & SupplierQueryParams
    ): Promise<APIResult<APIPaging<SupplierDTO>>> => {
        return http.get('/supplier/list', { params })
    },

    // 获取供应商详情
    getDetail: (id: string): Promise<APIResult<SupplierDTO>> => {
        return http.get(`/supplier/detail/${id}`)
    },

    // 创建供应商
    create: (data: SupplierCreateParams): Promise<APIResult<string>> => {
        return http.post('/supplier/create', data)
    },

    // 更新供应商
    update: (
        id: string,
        data: Partial<SupplierCreateParams>
    ): Promise<APIResult<void>> => {
        return http.patch(`/supplier/update/${id}`, data)
    },

    // 更新供应商状态
    updateStatus: (
        id: string,
        status: 'active' | 'inactive' | 'blacklisted'
    ): Promise<APIResult<void>> => {
        return http.post(`/supplier/update-status/${id}`, { status })
    },

    // 关联物料
    linkMaterial: (
        supplierId: string,
        materialCode: string
    ): Promise<APIResult<void>> => {
        return http.post(`/supplier/link-material/${supplierId}`, { materialCode })
    },

    // 获取供应商可选物料
    getAvailableMaterials: (
        supplierId: string
    ): Promise<APIResult<string[]>> => {
        return http.get(`/supplier/available-materials/${supplierId}`)
    }
}